{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert Keras model to Tensorflow model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Keras model is successfully converted to TF model in ./model/tensorflow/small/model_small\n"
     ]
    }
   ],
   "source": [
    "# import the needed libraries\n",
    "import tensorflow as tf\n",
    "tf.keras.backend.set_learning_phase(0) #use this if we have batch norm layer in our network\n",
    "from tensorflow.keras.models import load_model\n",
    "\n",
    "# path we wanna save our converted TF-model\n",
    "#MODEL_PATH = \"./model/tensorflow/big/model1\"\n",
    "MODEL_PATH = \"./model/tensorflow/small/model_small\"\n",
    "\n",
    "# load the Keras model\n",
    "#model = load_model('./model/modelLeNet5.h5')\n",
    "model = load_model('./model/modelLeNet5_small.h5')\n",
    "\n",
    "# save the model to Tensorflow model\n",
    "saver = tf.train.Saver()\n",
    "sess = tf.keras.backend.get_session()\n",
    "save_path = saver.save(sess, MODEL_PATH)\n",
    "\n",
    "print(\"Keras model is successfully converted to TF model in \"+MODEL_PATH)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Keras to TensorRT\n",
    "![alt text](pictures/Keras_to_TensorRT.png)\n",
    "\n",
    "### Tensorflow to TensorRT\n",
    "![alt text](pictures/tf-trt_workflow.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:TF1120_GPU]",
   "language": "python",
   "name": "conda-env-TF1120_GPU-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
